import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import { useStore } from '@/store';
import { done, start } from '@/plugins/nprogress';
const routes: RouteRecordRaw[] = [
    // {
    //     path: '/',
    //     name: 'home',
    //     meta: {
    //         title: '主页',
    //         requireAuth: false,
    //     },
    //     component: () => import('@/views/home/Home.vue'),
    // },
    // {
    //     path: '/login',
    //     name: 'login',
    //     meta: {
    //         title: '登录',
    //         requireAuth: false,
    //     },
    //     component: () => import('@/views/login/Login.vue'),
    // },
    {
        path: '/deathInformationEdit',
        name: 'deathInformationEdit',
        meta: {
            title: '死亡信息编辑',
            requireAuth: false,
        },
        component: () => import('@/views/deathInformationEdit/DeathInformationEdit.vue'),
    },
    {
        path: '/cremationAppointment',
        name: 'cremationAppointment',
        meta: {
            title: '火化预约',
            requireAuth: false,
        },
        component: () => import('@/views/cremationAppointment/CremationAppointment.vue'),
    },
    // 将匹配所有内容并将其放在 `route.params.pathMatch` 下
    {
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        meta: {
            title: '404',
            requireAuth: false,
        },
        component: () => import('@/views/404/404.vue'),
    },
];
console.log(import.meta.env.BASE_URL);
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    // history: createWebHistory('/'),
    // 路由跳转之后滚动条滚到顶部
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition;
        } else {
            // return { x: 0, y: 0 } vue2这种，vue3改成top
            return { top: 0 };
        }
    },
    routes,
});

//路由判断登录 根据路由配置文件的参数
router.beforeEach((to, from, next) => {
    // 开启进度条
    start();
    //改title
    if (to.meta.title) {
        document.title = to.meta.title as string;
    }
    // next();
    console.log(to.matched);
    if (to.matched.some((record) => record.meta.requireAuth)) {
        //判断该路由是否需要登录权限
        console.log('需要登录');
        let store = useStore();
        console.log(store.openid);
        if (store.openid) {
            //判断当前的token是否存在 ； 登录存入的token
            next();
        } else {
            next({
                path: '/login',
                query: {
                    redirect: to.fullPath,
                }, //将跳转的路由path作为参数，登录成功后跳转到该路由
            });
        }
    } else {
        next();
    }
    // if (to.matched.some(record => record.meta.requireAuth)) { //判断该路由是否需要登录权限
    //     console.log('需要登录');
    //     if (sessionStorage.token) { //判断当前的token是否存在 ； 登录存入的token
    //         next();
    //     } else {
    //         next({
    //             path: '/login',
    //             query: {
    //                 redirect: to.fullPath
    //             } //将跳转的路由path作为参数，登录成功后跳转到该路由
    //         })
    //     }
    // } else {
    //     next();
    // }
});
// 路由后置后卫
router.afterEach(() => {
    // 关闭进度条
    done();
});
export default router;
